import requests, os, pymongo, re, csv, time
from lxml import etree

daan_url_list = []
headers = {
    "User-Agent": "Mozilla / 5.0(WindowsNT 10.0;WOW64) AppleWebKit / 537.36(KHTML, like Gecko) Chrome / 67.0.3396.99 Safari / 537.36",
    'Cookie': 'sessionid=4ik8w72rp761d5dft6epvrv6zwet6qom',
    'X-DevTools-Emulate-Network-Conditions-Client-Id': '7CC3B2E78B57C0226E1BE40E010F7249'
}
# 学科
print("获取学科信息……")
index_url = "http://42.51.0.42/"
headers['Referer'] = "http://42.51.0.42/login/"
response = requests.get(url=index_url, headers=headers)
print(response.encoding)
# response.encoding = response.apparent_encoding
response.encoding = response.apparent_encoding
# print(response.text)
html = etree.HTML(response.text)
xueke_list = html.xpath("//li[@class='nav-item']/a[@class='item-anchor']")
# print(len(xueke_list))
# print(xueke_list)

# 遍历学科，获取单元
print("获取单元信息……")
for i in xueke_list:
    xueke_name = i.xpath("./text()")[0].strip()
    xueke_url = "http://42.51.0.42" + i.xpath("./@href")[0] + "/"
    # 以学科创建文件夹----
    if not os.path.exists(xueke_name):
        os.mkdir(xueke_name)
    print(xueke_name, ":", xueke_url)
    headers['Referer'] = index_url
    # print(headers)
    res = requests.get(url=xueke_url, headers=headers)
    # print(res.text)
    res.encoding = response.apparent_encoding
    html = etree.HTML(res.text)
    danyuan_list = html.xpath("//li[@class='nav-item']/a[@class='item-anchor']")
    # print(len(danyuan_list))
    # print(danyuan_list)

    if len(danyuan_list) == 0:
        continue
    # 遍历单元，获取内容
    print("获取单元考题……")
    for i in danyuan_list:
        danyuan_name = i.xpath("./text()")[0].split(".")[0].strip()
        content_url = "http://42.51.0.42" + i.xpath("./@href")[0]
        headers['Referer'] = xueke_url
        # print(headers)
        resp = requests.get(url=content_url, headers=headers)
        # print(resp.text)
        resp.encoding = response.apparent_encoding
        html = etree.HTML(resp.text)
        content_list = html.xpath("//div[@class='card']")
        # 拼接答案的字符串--
        daan = "http://42.51.0.42/finish/?"

        for i in content_list:
            bianhao = i.xpath("./p[@class='question']/span/text()")[0]
            bianhao = re.findall("\d+", bianhao)[0]
            leixing = i.xpath("./p[@class='question']/span/text()")[0].split(":")[-1]
            tigan = i.xpath("./p[@class='question']/text()")[0]
            # print(leixing)
            A = i.xpath("./ul/li[1]/label/text()")[0]
            B = i.xpath("./ul/li[2]/label/text()")[0]
            C = i.xpath("./ul/li[3]/label/text()")[0]
            D = i.xpath("./ul/li[4]/label/text()")[0]
            T = i.xpath("./ul/input/@value")[0]
            # print(bianhao)

            # 获取题目的name值-----------
            ti_name = i.xpath("./ul/li[2]/input/@name")[0]
            "choiceT=A&2971=C&"
            daan += "choiceT={}&{}={}&".format(T, ti_name, T)

            # # 方法一：存入csv文件，写表头
            openfile = open(xueke_name + "/" + danyuan_name + ".csv", "a+", encoding='utf-8', newline="")
            fieldnames = ['序号', '类型', '题干', '选项A', '选项B', '选项C', '选项D', '标准答案']
            w = csv.DictWriter(openfile, fieldnames=fieldnames)
            # 题目编号为1时，先写入表头
            if bianhao == '1':
                w.writeheader()
            # 题目数据存入csv文件：
            # w.writerow([bianhao,leixing,tigan,A,B,C,D,T])
            w.writerow({
                '序号': bianhao,
                '类型': leixing,
                '题干': tigan,
                '选项A': A,
                '选项B': B,
                '选项C': C,
                '选项D': D,
                '标准答案': T,
            })

            # # 方法二：列表方法写入
            # openfile = open(xueke_name + "/" + danyuan_name + ".csv", "a", encoding='gb2312',newline="")
            # fieldnames = ['序号', '类型', '题干', '选项A', '选项B', '选项C', '选项D', '标准答案']
            # writer = csv.writer(openfile,dialect='excel')
            # # 题目编号为1时，先写入表头
            # if bianhao == '1':
            #     writer.writerow(fieldnames)
            # writer.writerow([bianhao,leixing,tigan,A,B,C,D,T])

        # 完整链接的拼接
        daan += "begin_time=2019-01-20+04%3A37%3A53&title_id={}".format(content_url.split("/")[-1])
        # 将编成的答案链接存储到列表中[学科名单元名，答案链接，referer链接]
        daan_url_list.append([xueke_name + danyuan_name, daan, content_url])
        print(daan)
        # 将答案连接写入文件
        with open('danan_url.txt', 'a+', encoding='utf-8') as fp:
            fp.write(daan + "\n\n")

# 携带用户cookie，当问正确答案链接
for i in daan_url_list:
    name = i[0]  # 学科名+单元名
    url = i[1]  # 答案链接
    referer = i[2]  # referer
    headers['Referer'] = url
    # headers['Cookie'] = ''
    response = requests.get(url=url, headers=headers)

#
# # 获取全部答对的页面截图
# from selenium import webdriver
# print(len(daan_url_list),"0000000000000000000000")
# for i in daan_url_list:
#     name = i[0]
#     url  = i[1]
#     referer = i[2]
#     print("---------",name,url,referer)
#     # 不包含此链接，说明没有题目
#     if url.find('choiceT') < 0:
#         continue
#     # 获得设置选项
#     options = webdriver.ChromeOptions()
#     # options.add_argument('User-Agent="Mozilla / 5.0(WindowsNT 10.0;WOW64) AppleWebKit / 537.36(KHTML, like Gecko) Chrome / 67.0.3396.99 Safari / 537.36"')
#     options.add_argument('Referer=%s'%referer)
#     print("===",options.arguments)
#     # 设置请求头
#     # options.add_argument()
#     # 驱动存储路径
#     path = r"D:\ProgramSoftware\chromedriver_win32\chromedriver.exe"
#     browser = webdriver.Chrome(executable_path=path, chrome_options=options)
#     browser.get(url)
#     browser.add_cookie(cookie_dict={'sessionid':'g3daj0i90clm37a80xfa0u46k46d30wo'})
#     browser.get(url)
#     time.sleep(1)
#     print(name)
#     browser.save_screenshot(name+".jpg")
#     print("截图完成")
